package ch.il07.converter.persistence.impl.mysql;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import ch.il07.converter.persistence.Persistence;
import ch.il07.converter.persistence.entities.User;
import ch.il07.converter.persistence.exceptions.PersistenceException;
import ch.il07.converter.persistence.impl.mysql.pooling.DbConnectionPoolPair;
import ch.il07.utils.ConverterProperties;

public abstract class AbstractMySQLPersistence implements Persistence {
	
	public static final String MYSQL_POOL = "MySQL";
	
	/**
	 * 
	 * @param user can be null
	 * @return either 'userID = XX' OR 'userID IS NULL'
	 */
	protected String createUserIDStmt(User user) {
		StringBuilder stmt = new StringBuilder("userID ");
		if(user == null) {
			stmt.append("IS NULL");
		} else {
			stmt.append("= " + user.getUserID());
		}
		return stmt.toString();
	}

	/**
	 * 
	 * @see http://board.gulli.com/thread/285455-java-string-per-md5-hashen/
	 * 
	 * @author acidjnk (at) gulli (dot) com
	 * 
	 * @param input
	 * 
	 * @return md5 hash of the input
	 * 
	 * @throws PersistanceException
	 *             on any error
	 */
	protected final String md5(String input) throws PersistenceException {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			md5.update(input.getBytes());
			byte[] result = md5.digest();

			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < result.length; i++) {
				if (result[i] <= 15 && result[i] >= 0) {
					hexString.append("0");
				}
				hexString.append(Integer.toHexString(0xFF & result[i]));
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			throw new PersistenceException("unable to make an md5 hash", e);
		}
	}
	
	/**
	 * @desc reads the configuration for mysql out of the property-file
	 * @return DbConnectionPoolPair for MySQL
	 * @throws RuntimeException
	 *             on missing configuration
	 */
	protected DbConnectionPoolPair getMySQLPool() {

		String host = ConverterProperties.getProperty("mysql_host");
		String port = ConverterProperties.getProperty("mysql_port");
		String user = ConverterProperties.getProperty("mysql_user");
		String pass = ConverterProperties.getProperty("mysql_pass");
		String db = ConverterProperties.getProperty("mysql_db");

		Integer maxConnections = null;
		try {
			maxConnections = Integer.parseInt(ConverterProperties
					.getProperty("mysql_maxConnections"));
		} catch (NumberFormatException e) {
		}

		if (host == null) {
			throw new RuntimeException("mysql_host missing");
		} else if (port == null) {
			throw new RuntimeException("mysql_port missing");
		} else if (user == null) {
			throw new RuntimeException("mysql_user missing");
		} else if (pass == null) {
			throw new RuntimeException("mysql_pass missing");
		} else if (db == null) {
			throw new RuntimeException("mysql_db missing");
		} else if (maxConnections == null) {
			throw new RuntimeException("mysql_maxConnections is missing");
		}

		String dbUrl = "jdbc:mysql://" + host + ":" + port + "/" + db;
		return new DbConnectionPoolPair(dbUrl, user, pass, maxConnections);
	}
}
